package main

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

type MyNode struct {
	tree    *TreeNode
	visited bool
}

func postorderTraversal(root *TreeNode) []int {
	stack := []*MyNode{
		{tree: root, visited: false},
	}
	var result []int
	for len(stack) > 0 {
		top := stack[len(stack)-1]
		stack = stack[0 : len(stack)-1]
		if top == nil || top.tree == nil {
			continue
		}
		if !top.visited {
			stack = append(stack, &MyNode{tree: top.tree, visited: true})
			stack = append(stack, &MyNode{tree: top.tree.Right, visited: false})
			stack = append(stack, &MyNode{tree: top.tree.Left, visited: false})
		} else {
			result = append(result, top.tree.Val)
		}
	}
	return result
}

func main() {

}
